package com.smh.demo1.a05;

import com.zaxxer.hikari.util.DriverDataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;

import javax.sql.DataSource;
import java.util.Properties;

/**
 * @author shiminghui
 * @date 2025/4/11 16:41
 * @description: TODO
 */
@ComponentScan(basePackages = "com.smh.demo1.a05.component")
@MapperScan(basePackages = "com.smh.demo1.a05.mapper")
public class Config {
    private Logger log = LoggerFactory.getLogger(Config.class);

    public Config() {
        log.info("Config init");
    }

    @Bean
    public Bean3 bean3() {
        log.info("bean3 init");
        return new Bean3();
    }

    @Bean
    public Bean4 bean4() {
        log.info("bean4 init");
        return new Bean4();
    }

    @Bean
    public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource) {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        return sqlSessionFactoryBean;
    }

    @Bean(initMethod = "getConnection") // 创建后自动调用该类的getConnection方法
    public DataSource dataSource() {
        String url = "jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC";
        String username = "root";
        String password = "422199";
        String DriverClassName = "com.mysql.cj.jdbc.Driver";
        return new DriverDataSource(url, DriverClassName, new Properties(), username, password);
    }

/*    @Bean
    public MapperFactoryBean<Mapper1> mapper1(SqlSessionFactory sqlSessionFactory) {
        MapperFactoryBean<Mapper1> mapperFactoryBean = new MapperFactoryBean<>(Mapper1.class);
        mapperFactoryBean.setSqlSessionFactory(sqlSessionFactory);
        return mapperFactoryBean;
    }

    @Bean
    public MapperFactoryBean<Mapper2> mapper2(SqlSessionFactory sqlSessionFactory) {
        MapperFactoryBean<Mapper2> mapperFactoryBean = new MapperFactoryBean<>(Mapper2.class);
        mapperFactoryBean.setSqlSessionFactory(sqlSessionFactory);
        return mapperFactoryBean;
    }*/

}
